// const autojsUtils = require('./modules/autojs-utils');
"ui";

const Constant = require("./constant.js");

auto();

device.keepScreenOn();

// 之前保存的钱包
Constant.initMtnAndPin();

// 是否显示钱包
const showPinInput = Constant.isNeedPinInput();
console.log("showPinInput: " + showPinInput);

// 防止重复启动
let canStartScript = true;

// 点击展示pin入口次数
let resetPinClickTimes = 0;

ui.layout(
    <frame h="*" w="*" bg="#FFFFFF">
        <vertical>
            <text id="sceneName"
                w="*"
                h="auto"
                gravity="center"
                marginTop="16"
                textColor="red"
                textStyle="bold"
                textSize="16"
                text={Constant.WALLET_MTN_GHS_NAME} />

            <input id="wallet"
                marginTop="16"
                hint="请输入钱包号码, 带0开头"
                text={Constant.getMtn()}
                textSize="14" />

            <input id="pin"
                marginTop="16"
                hint="请输入钱包PIN"
                text={Constant.getPin()}
                visibility={showPinInput}
                textSize="14" />

            <button id="saveAndStart"
                marginTop="16"
                text="启动脚本（不要重复点击）" />
        </vertical>

        <button id="resetPin"
            layout_gravity="right|bottom"
            w="auto" h="auto"
            marginBottom="40"
            marginRight="10"
            bg="#00000000"
            textColor="#00000000"
            textSize="8sp"
            text="重置pin" />
    </frame>
);

ui.resetPin.click(function () {
    resetPinClickTimes += 1;
    // console.log("resetPinClickTimes:" + resetPinClickTimes);

    if ((resetPinClickTimes > 0) && (resetPinClickTimes % 3 === 0)) {
        resetPinClickTimes = 0;
        ui.pin.setVisibility(android.view.View.VISIBLE);
    }
});

ui.saveAndStart.click(function () {
    // 防止重复启动
    if (!canStartScript) {
        console.log("canStartScript: " + canStartScript);
        return;
    }
    canStartScript = false;

    var name = ui.wallet.text();
    var pin = ui.pin.text();

    if (!name || (name.length <= 8)) {
        canStartScript = true;
        toast("钱包号错误!");
        return;
    }

    if (!pin || (pin.length !== 4)) {
        canStartScript = true;
        toast("pin错误!");
        return;
    }

    // 弹框确认
    confirm("务必确认PIN码 (第1次)", "号码:" + name + "\nPIN:" + pin).then(value => {
        //当点击确定后会执行这里, value为true或false, 表示点击"确定"或"取消"
        if (value) {
            confirm("务必确认PIN码 (第2次)", "号码:" + name + "\nPIN:" + pin).then(value => {
                //当点击确定后会执行这里, value为true或false, 表示点击"确定"或"取消"
                if (value) {
                    confirm("务必确认PIN码 (第3次)", "号码:" + name + "\nPIN:" + pin).then(value => {
                        //当点击确定后会执行这里, value为true或false, 表示点击"确定"或"取消"
                        if (value) {
                            confirm("务必确认PIN码 (第4次)", "号码:" + name + "\nPIN:" + pin).then(value => {
                                //当点击确定后会执行这里, value为true或false, 表示点击"确定"或"取消"
                                if (value) {
                                    confirm("务必确认PIN码 (第5次)", "号码:" + name + "\nPIN:" + pin).then(value => {
                                        //当点击确定后会执行这里, value为true或false, 表示点击"确定"或"取消"
                                        if (value) {
                                            // 保存本地
                                            Constant.setMtn(name);
                                            Constant.setPin(pin);

                                            // 隐藏pin
                                            ui.pin.setVisibility(android.view.View.GONE);

                                            // toast(name + "," + pin + " 已启动脚本");

                                            setTimeout(() => {
                                                // 启动脚本
                                                threads.start(function () {
                                                    canStartScript = true;
                                                    //在新线程执行的代码
                                                    main();
                                                });
                                            }, 1000);
                                        } else {
                                            canStartScript = true;
                                        }
                                    });
                                } else {
                                    canStartScript = true;
                                }
                            });
                        } else {
                            canStartScript = true;
                        }
                    });
                } else {
                    canStartScript = true;
                }
            });
        } else {
            canStartScript = true;
        }
    });
});


const MomoGhsAppOpen = require("./momoGhsAppOpenV2.js");
const MomoGhsPageUtil = require("./momoGhsPageUtilV2.js");
const SmsUtil = require("./baseSmsUtil.js");
const ApiUrl = require("./baseApiUrl.js");
const HttpUtil = require("./baseHttpUtil.js");
// const Constant = require("./constant.js");
const RunDefine = require("./baseRunDefine.js");
const MomoGhsDoLogin = require("./momoGhsDoLoginV2.js");
const MomoGhsDoTransfer = require("./momoGhsDoTransferV2.js");
const MomoGhsFetchBills = require("./momoGhsFetchBillsV2.js");

// 转账信息
let targetMtnTransferAmount = "";
let targetMtnTransferId = "";
let targetMtnTransferAccount = "";
let targetMtnTransferReference = "";

const initMtnTransfer = (data) => {
    targetMtnTransferAmount = data.amount + "";
    targetMtnTransferId = data.id + "";
    targetMtnTransferAccount = data.mtn + "";
    targetMtnTransferReference = data.reference + "";
}

const isValidMtnTransfer = () => {
    let result1 = targetMtnTransferAmount || "";
    let result2 = targetMtnTransferId || "";
    let result3 = targetMtnTransferAccount || "";

    return ((result1.length > 0) && (result2.length > 0) && (result3.length > 0));
}

const cleanMtnTransfer = () => {
    targetMtnTransferAmount = "";
    targetMtnTransferId = "";
    targetMtnTransferAccount = "";
    targetMtnTransferReference = "";
}

const printMtnTransferLog = () => {
    console.log(Constant.UI_SDK_VERSION + "targetMtnTransferAmount: " + targetMtnTransferAmount + ",\ntargetMtnTransferId: " + targetMtnTransferId + ",\ntargetMtnTransferAccount: " + targetMtnTransferAccount + ",\ntargetMtnTransferReference: " + targetMtnTransferReference);
}


/** 启动脚本逻辑 */
function main() {

    // 清空转账缓存
    cleanMtnTransfer();

    // 程序结束自动 隐藏控制台
    console.show(false);
    console.setMaxLines(40);

    // 上传短信
    SmsUtil.syncSmsData();

    console.log(Constant.UI_SDK_VERSION + "当前设备: " + Constant.getMtn() + " PIN: " + Constant.getPin());
    sleep(5 * 1000);
    MomoGhsAppOpen.openMomoApp();

    // 上传短信
    let lastSmsList = [];
    SmsUtil.syncSmsData(true);
    sleep(5 * 1000);

    // login com.comviva.webaxn.ui.WebAxnActivity
    // pin com.comviva.webaxn.ui.WebAxnActivity
    // com.comviva.webaxn.ui.WebAxnActivity
    // com.comviva.webaxn.ui.WebAxnActivity
    // com.comviva.webaxn.ui.WebAxnActivity
    // com.comviva.webaxn.ui.WebAxnActivity

    // android.app.Dialog
    // android.view.View

    let lastTimeMillis = 0;
    const FIVE_MINUTES_IN_MILLIS = 5 * 60 * 1000;

    while (true) {
        // 清空控制台
        console.clear();

        // 清空转账信息
        cleanMtnTransfer();

        // 检查是否需要更新
        if (MomoGhsPageUtil.isUpdatePage()) {
            MomoGhsPageUtil.doSkipUpdateNow();
        }

        // 页面状态
        let pageStatus = MomoGhsPageUtil.doCheckPageStatus();
        let isLoginPage = pageStatus.isLoginPage;
        let isPinPage = pageStatus.isLoginPinPage;
        let isHomePage = pageStatus.isHomePage;
        let isSendMoneyPage = pageStatus.isSendMoneyPage;
        let isSendMoneyAccountInputPage = pageStatus.isSendMoneyAccountInputPage;
        let isSendMoneyConfirmPage = pageStatus.isSendMoneyConfirmPage;
        let isSendMoneySuccessPage = pageStatus.isSendMoneySuccessPage;
        let isSendMoneyFailPage = pageStatus.isSendMoneyFailPage;
        let isBillSeeAllPage = pageStatus.isBillSeeAllPage;
        let isBillListPage = pageStatus.isBillListPage;

        // 初始化页面标识
        // isLoginPage = MomoGhsPageUtil.isLoginPage();
        // isPinPage = MomoGhsPageUtil.isLoginPinPage();
        // isHomePage = MomoGhsPageUtil.isHomePage();
        // isBillSeeAllPage = MomoGhsPageUtil.isSelectAllPage();
        // isBillListPage = MomoGhsPageUtil.isTransactionListPage();

        if (isBillSeeAllPage) {
            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }
            // 返回首页
            back();
            sleep(1000);

        } else if (isBillListPage) {
            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }
            // 返回首页
            back();
            sleep(1000);

        } else if (isSendMoneyPage) {
            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }
            // 返回首页
            back();
            sleep(1000);

        } else if (isSendMoneyAccountInputPage) {
            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }
            // 返回首页
            click(10, 1600);
            back();
            sleep(1000);

        } else if (isSendMoneyConfirmPage) {
            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }
            // 返回首页
            back();
            back();
            sleep(1000);

        } else if (isSendMoneySuccessPage) {
            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }
            // 返回首页
            back();
            sleep(1000);
        }

        // 上传短信
        SmsUtil.syncSmsData();
        sleep(1 * 1000);

        // 判断是否为登录-手机号页
        if (isLoginPage) {
            // 清空控制台
            console.clear();

            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }

            // 走登录-手机号流程
            let doLoginResult = MomoGhsDoLogin.doLogin();
            if (doLoginResult) {
                // 循环判断
                // 判断是否为 pin 页
                for (let pinIndex = 0; pinIndex < RunDefine.loopQueryCount; pinIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((pinIndex + 1) % 20) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let pinQueryResult = MomoGhsPageUtil.isLoginPinPage();
                    if (pinQueryResult) {
                        isPinPage = true;
                        break;
                    }
                    sleep(1 * 1000);
                }
            }

            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }
        }

        // 判断是否为登录-PIN页
        if (isPinPage) {
            // 清空控制台
            console.clear();

            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }

            // 走登录-PIN流程
            let doPinResult = MomoGhsDoLogin.doPin();
            if (doPinResult) {
                // 循环判断
                // 判断是否为 首页
                for (let homeIndex = 0; homeIndex < RunDefine.loopQueryCount; homeIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((homeIndex + 1) % 20) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let homeQueryResult = MomoGhsPageUtil.isHomePage();
                    if (homeQueryResult) {
                        isHomePage = true;
                        break;
                    }
                    sleep(1 * 1000);
                }
            }

            // 检查是否需要更新
            if (MomoGhsPageUtil.isUpdatePage()) {
                MomoGhsPageUtil.doSkipUpdateNow();
            }
        }

        // 判断是否为首页
        if (isHomePage) {
            // 清空控制台
            console.clear();

            // 获取转账信息
            cleanMtnTransfer();

            let url = ApiUrl.getMtnTransferAccount();
            let mtnAccountParams = { num: "1", phone: Constant.getMtn() }
            let mtnAccountResult = HttpUtil.sendPostSync(url, mtnAccountParams)
            // let mtnAccountResult = [{ amount: "1", id: "test_id_537150226", mtn: "537993253", reference: "test_ref_537150226" }]

            if (mtnAccountResult !== null) {
                if (mtnAccountResult.length > 0) {
                    initMtnTransfer(mtnAccountResult[0]);
                } else {
                    cleanMtnTransfer();
                }
            } else {
                cleanMtnTransfer();
            }

            // 清空控制台
            console.clear();

            // 打印转账信息
            printMtnTransferLog();
        }

        // 判断是否需要转账
        if (isValidMtnTransfer()) {
            // 走打开 Send Money 流程
            let doOpenSendMoneyPageResult = MomoGhsDoTransfer.doOpenSendMoneyPage();
            if (doOpenSendMoneyPageResult) {
                // 循环判断
                // 判断是否为 Send Money 页
                for (let sendMoneyIndex = 0; sendMoneyIndex < RunDefine.loopQueryCount; sendMoneyIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((sendMoneyIndex + 1) % 20) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let sendMoneyQueryResult = MomoGhsPageUtil.isSendMoneyMtnPage();
                    if (sendMoneyQueryResult) {
                        isSendMoneyPage = true;
                        isHomePage = false;
                        break;
                    }
                    sleep(1 * 1000);
                }
            } else {
                // 保存转账失败
                let url = ApiUrl.saveMtnTransferResult();
                let mtnTransferResultParams = { id: targetMtnTransferId, response: "", status: "3", txnId: "" }
                HttpUtil.sendPostSync(url, mtnTransferResultParams)
                sleep(1 * 1000);
            }
        }

        // 判断是否是 Send Money 页
        if (isSendMoneyPage && isValidMtnTransfer()) {
            // 清空控制台
            console.clear();

            // 走打开 To MTN MoMo 流程
            let doOpenMtnMoMoPageResult = MomoGhsDoTransfer.doOpenMtnMoMoPage();
            if (doOpenMtnMoMoPageResult) {
                // 循环判断
                // 判断是否为 To MTN MoMo 页
                for (let mtnMoMoIndex = 0; mtnMoMoIndex < RunDefine.loopQueryCount; mtnMoMoIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((mtnMoMoIndex + 1) % 20) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let mtnMoMoAccountInputQueryResult = MomoGhsPageUtil.isSendMoneyMtnAccountInputPage();
                    if (mtnMoMoAccountInputQueryResult) {
                        isSendMoneyAccountInputPage = true;
                        isHomePage = false;
                        break;
                    }
                    sleep(1 * 1000);
                }
            } else {
                // 保存转账失败
                let url = ApiUrl.saveMtnTransferResult();
                let mtnTransferResultParams = { id: targetMtnTransferId, response: "", status: "3", txnId: "" }
                HttpUtil.sendPostSync(url, mtnTransferResultParams)
                sleep(1 * 1000);
            }
        }

        // 判断是否为转账 account input 页
        if (isSendMoneyAccountInputPage && isValidMtnTransfer()) {
            // 清空控制台
            console.clear();

            // 开始转账流程
            let doMtnMoMoAccountInputResult = MomoGhsDoTransfer.doMtnMoMoAccountInput(targetMtnTransferAccount, targetMtnTransferAmount, targetMtnTransferReference);
            if (doMtnMoMoAccountInputResult) {
                // 循环判断
                // 判断是否为 Mtn MoMo account confirm 页
                for (let mtnMoMoAccountInputIndex = 0; mtnMoMoAccountInputIndex < RunDefine.loopQueryCount; mtnMoMoAccountInputIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((mtnMoMoAccountInputIndex + 1) % 20) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let mtnMoMoAccountConfirmQueryResult = MomoGhsPageUtil.isSendMoneyMtnAccountConfirmPage();
                    if (mtnMoMoAccountConfirmQueryResult) {
                        isSendMoneyConfirmPage = true;
                        isHomePage = false;
                        break;
                    }
                    sleep(1 * 1000);
                }
            } else {
                // 保存转账失败
                let url = ApiUrl.saveMtnTransferResult();
                let mtnTransferResultParams = { id: targetMtnTransferId, response: "", status: "3", txnId: "" }
                HttpUtil.sendPostSync(url, mtnTransferResultParams)
                sleep(1 * 1000);
            }
        }

        // 判断是否为转账 Mtn account confirm 页
        if (isSendMoneyConfirmPage) {

            // 点击确认
            let doClickTransferComletedResult = MomoGhsDoTransfer.doClickTransferComleted();
            if (doClickTransferComletedResult) {
                // 循环判断
                // 判断是否为 Mtn MoMo transfer success/fail 页
                for (let mtnMoMoTransferCompleteIndex = 0; mtnMoMoTransferCompleteIndex < RunDefine.loopQueryCount; mtnMoMoTransferCompleteIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((mtnMoMoTransferCompleteIndex + 1) % 20) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    // success
                    let mtnMoMoTransferSuccessQueryResult = MomoGhsPageUtil.isSendMoneyMtnSuccessPage();
                    if (mtnMoMoTransferSuccessQueryResult) {
                        isSendMoneySuccessPage = true;
                        isSendMoneyFailPage = false;
                        isHomePage = false;
                        break;
                    }

                    // fail
                    let mtnMoMoTransferFailQueryResult = MomoGhsPageUtil.isSendMoneyMtnFailPage();
                    if (mtnMoMoTransferFailQueryResult) {
                        isSendMoneySuccessPage = false;
                        isSendMoneyFailPage = true;
                        isHomePage = false;
                        break;
                    }

                    sleep(1 * 1000);
                }
            } else {
                // 保存转账失败
                let url = ApiUrl.saveMtnTransferResult();
                let mtnTransferResultParams = { id: targetMtnTransferId, response: "", status: "3", txnId: "" }
                HttpUtil.sendPostSync(url, mtnTransferResultParams)
                sleep(1 * 1000);
            }
        }

        // 判断是否为转账 Mtn momo transfer success 页
        if (isSendMoneySuccessPage) {

            isSendMoneyFailPage = false;
            isHomePage = false;

            // 保存转账成功
            let url = ApiUrl.saveMtnTransferResult();
            let mtnTransferResultParams = { id: targetMtnTransferId, response: "", status: "2", txnId: "" }
            HttpUtil.sendPostSync(url, mtnTransferResultParams)
            sleep(1 * 1000);

            // 清空转账信息
            cleanMtnTransfer();

            // 完成转账流程，返回 首页
            let doClickTransferDoneAndGoBackResult = MomoGhsDoTransfer.doClickTransferDoneAndGoBack();
            if (doClickTransferDoneAndGoBackResult) {
                // 循环判断
                // 判断是否为 Send Money 页
                for (let clickTransferDoneAndGoBackIndex = 0; clickTransferDoneAndGoBackIndex < RunDefine.loopQueryCount; clickTransferDoneAndGoBackIndex++) {

                    // 计算公式： (n+1)%10 = 0 时上报短信
                    if (((clickTransferDoneAndGoBackIndex + 1) % 20) === 0) {
                        // 上传短信
                        SmsUtil.syncSmsData();
                    }

                    let sendMoneyQueryResult = MomoGhsPageUtil.isSendMoneyMtnPage();
                    if (sendMoneyQueryResult) {
                        // 返回
                        back();
                        sleep(1 * 1000);
                        break;
                    }
                    sleep(1 * 1000);
                }
            }
        }

        // 判断是否为转账 Mtn momo transfer fail 页
        if (isSendMoneyFailPage) {

            isSendMoneySuccessPage = false;
            isHomePage = false;

            // 保存转账失败
            let url = ApiUrl.saveMtnTransferResult();
            let mtnTransferResultParams = { id: targetMtnTransferId, response: "", status: "3", txnId: "" }
            HttpUtil.sendPostSync(url, mtnTransferResultParams)
            sleep(1 * 1000);

            // 清空转账信息
            cleanMtnTransfer();

            // 完成转账流程，需要重启
        }

        // 循环判断
        // 判断是否为 首页
        for (let homeIndex = 0; homeIndex < RunDefine.loopQueryCount; homeIndex++) {

            // 计算公式： (n+1)%10 = 0 时上报短信, 这里不用上传短信
            // if (((homeIndex + 1) % 10) === 0) {
            //     // 上传短信
            //     SmsUtil.syncSmsData();
            // }

            isHomePage = MomoGhsPageUtil.isHomePage();
            if (isHomePage) {
                isBillListPage = false;
                isBillSeeAllPage = false;
                break;
            }
            sleep(1 * 1000);
        }

        // 判断是否为首页
        if (isHomePage) {
            // 清空控制台
            console.clear();

            // 查询是否需要查流水
            let canCheckBillList = false;
            let needUploadBillsUrl = ApiUrl.checkNeedUploadBills();
            let isNeedUploadBillsParams = { phone: Constant.getMtn() }
            let isNeedUploadBillsResult = HttpUtil.sendPostSync(needUploadBillsUrl, isNeedUploadBillsParams)
            if (isNeedUploadBillsResult !== null) {
                canCheckBillList = isNeedUploadBillsResult.flag || false;
                console.log(Constant.UI_SDK_VERSION + "流水状态：" + canCheckBillList);
                // if (canCheckBillList === true) {
                //     curTimeMillis = new Date().getTime();
                //     if ((curTimeMillis - lastTimeMillis) < FIVE_MINUTES_IN_MILLIS) {
                //         canCheckBillList = false;
                //     }
                // }
                sleep(2000);
            }

            // 如果短信有变动，则重查流水
            if (canCheckBillList === true) {
                let doClickTransacitonsResult = MomoGhsFetchBills.doClickTransacitons();
                if (doClickTransacitonsResult) {
                    // 循环判断
                    // 判断是否为 Home 页
                    for (let billTransactionsIndex = 0; billTransactionsIndex < RunDefine.loopQueryCount; billTransactionsIndex++) {

                        // 计算公式： (n+1)%10 = 0 时上报短信
                        if (((billTransactionsIndex + 1) % 20) === 0) {
                            // 上传短信
                            SmsUtil.syncSmsData();
                        }

                        let billSelectAllQueryResult = MomoGhsPageUtil.isSelectAllPage();
                        if (billSelectAllQueryResult) {
                            isBillSeeAllPage = true;
                            break;
                        }
                        sleep(1 * 1000);
                    }
                }

                // 判断是否为 bill see all 页
                if (isBillSeeAllPage) {
                    // 清空控制台
                    console.clear();

                    let doClickSeeAllResult = MomoGhsFetchBills.doClickSeeAll();
                    if (doClickSeeAllResult) {
                        // 循环判断
                        // 判断是否为 bill list 页
                        for (let billSeeAllIndex = 0; billSeeAllIndex < RunDefine.loopQueryCount; billSeeAllIndex++) {

                            // 计算公式： (n+1)%10 = 0 时上报短信
                            if (((billSeeAllIndex + 1) % 20) === 0) {
                                // 上传短信
                                SmsUtil.syncSmsData();
                            }

                            let billListQueryResult = MomoGhsPageUtil.isTransactionListPage();
                            if (billListQueryResult) {
                                isBillListPage = true;
                                break;
                            }
                            sleep(1 * 1000);
                        }
                    }
                }

                // 判断是否为 bill list 页
                if (isBillListPage) {
                    // 清空控制台
                    console.clear();

                    // 获取流程详情
                    MomoGhsAppOpen.getBillDetails();

                    // 清空控制台
                    console.clear();

                    // 返回
                    back();
                    sleep(1 * 1000);

                    // 上传短信
                    lastSmsList.length = 0;
                    lastSmsList = null;
                    lastSmsList = SmsUtil.syncSmsData(true);

                    console.log(Constant.UI_SDK_VERSION + "上报结束, 12秒后重新查询")
                    continue;
                }
            } else {
                sleep(1000)
                click("Approvals");
                sleep(500)
                click("Approvals");

                console.log(Constant.UI_SDK_VERSION + "短信未发生变动，不查询流水")
                // continue;

                // 点击一下通知中心，以免登录session 过期
                // click(device.width - 90, 140);
                sleep(1000);

                // 点击一下通知中心，以免登录session 过期
                let doOpenApprovalsPageResult = MomoGhsPageUtil.doOpenApprovalsPage();
                if (doOpenApprovalsPageResult) {
                    back();
                    sleep(1000);

                    let doOpenHomePageResult = MomoGhsPageUtil.doOpenHomePage();
                    if (doOpenHomePageResult) {
                        sleep(1000);
                        continue;
                    }
                }
            }
        }

        // 清空控制台
        console.clear();

        sleep(1 * 1000);

        // 最后都不能匹配成功，
        // 走关闭流程
        MomoGhsAppOpen.closeMomoApp();

        // 走重启流程
        MomoGhsAppOpen.openMomoApp();

        // 清空控制台
        console.clear();
    }
}




